//
//  InfoMationViewController.m
//  DaleTou
//
//  Created by hejingjin on 2018/5/27.
//  Copyright © 2018年 geren. All rights reserved.
//

#import "InfoMationViewController.h"
#import "ZixunTableViewCell.h"
#import "InfoDetailViewController.h"
#import <MJRefresh.h>
#import "Appearance.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

@interface InfoMationViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, strong) NSArray *imgDataSource;

@end

@implementation InfoMationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithHexString:@"ffffff"];
    [self setNavConfigStartColor:@"#ff8e01" endColor:@"#ff8e01"];
    [self ApptitileView:@"资讯"];
    [self.view addSubview:self.tableView];
}

#pragma mark - UITableViewDelegate methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZixunTableViewCell *cell = [ZixunTableViewCell cellWithTableview:tableView];
    NSDictionary *dic = self.dataSource[indexPath.row];
    cell.titleLbl.text = dic[@"title"];
    cell.timeLbl.text = dic[@"time"];
    cell.imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"zixun_%ld",indexPath.row % 10 + 1]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic = self.dataSource[indexPath.row];
    InfoDetailViewController *vc = [[InfoDetailViewController alloc] init];
    vc.timeStr = dic[@"time"];
    vc.titleStr = dic[@"title"];
    vc.contentStr = dic[@"content"];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)loadMore{
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"zixunData" ofType:@"plist"];
    NSArray *arr  = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
    [self.dataSource addObjectsFromArray:arr];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView.mj_footer endRefreshing];
        [self.tableView reloadData];
    });
}

#pragma mark - lazy methods
- (UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NAV_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - NAV_HEIGHT - bottom_HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    }
    return _tableView;
}

- (NSMutableArray *)dataSource{
    if (_dataSource == nil) {
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"zixunData" ofType:@"plist"];
        _dataSource = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];

    }
    return _dataSource;
}

//- (NSArray *)imgDataSource{
//    if (_imgDataSource == nil) {
//        _imgDataSource = @[@"zixun_1",@"zixun_2",@"zixun_3",@"zixun_4",@"zixun_5",@"zixun_6",@"zixun_8",@"zixun_1",@"zixun_1",@"zixun_1",]
//    }
//}

@end
